package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Kyl
	 */
	public class BallLauncher extends FlxSprite 
	{		
		[Embed (source = 'balllauncher.png')]
		private var launcherArt:Class; 
		
		public function BallLauncher(xPos:Number, yPos:Number, xVel:Number, yVel:Number, id:int) 
		{
			super(xPos, yPos);
			// bounding box
            width = 30;
            height = 50;
			super.offset.x = width / 2 - 10;
			super.offset.y = height / 2;

            immovable = true;
            visible = true;
			solid = false;

            _Id = id;
			xVelocity = xVel;
			yVelocity = yVel;
            
            //makeGraphic(width, height, 0xff006060);
			loadGraphic(launcherArt, false, false, width, height, false);
			
			super.angle = Math.atan2(xVelocity,-yVelocity) * 180 / Math.PI;
			
			myBall = null;
			
			var magnitude:Number = Math.sqrt(xVel * xVel + yVel * yVel);
			
			ballOrigin = new FlxPoint();
			ballOrigin.x = super.x - (yVelocity / magnitude * offset.x);
			ballOrigin.y = super.y + (xVelocity / magnitude * offset.x);
			ballOrigin.x += xVelocity / magnitude * offset.y;
			ballOrigin.y += yVelocity / magnitude * offset.y;
		}
		
		// We will give these velocities to each ball we launch
		private var xVelocity:Number;
		private var yVelocity:Number;
		
		private var ballOrigin:FlxPoint;
		
		private var myBall:Ball;
		
		public function launch(lvl:Level) : void
		{
			// If we've already added our ball, just change its position and velocity
			if ( myBall )
			{
				lvl.removeBall(myBall);
				myBall = new Ball(ballOrigin.x, ballOrigin.y);
				myBall.velocity.x = xVelocity;
				myBall.velocity.y = yVelocity;
				myBall.acceleration.y = 300;
				lvl.addBall(myBall);
			}
			else
			{
				myBall = new Ball(ballOrigin.x, ballOrigin.y);
				myBall.velocity.x = xVelocity;
				myBall.velocity.y = yVelocity;
				myBall.acceleration.y = 300;
				lvl.addBall(myBall);
			}
		}
		
		// Our ID
		private var _Id:int;
		
		public function getId() : Number
		{
			return _Id;
		}
	}
}